//
//  GQHAuthorizationController.m
//  Xanthium
//
//  Created by GuanQinghao on 31/03/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#pragma mark Macro
#import "GQHHeader.h"

#pragma mark Model

#pragma mark Object

#pragma mark View
#import "GQHAuthorizationView.h"

#pragma mark Controller
#import "GQHAuthorizationController.h"

#import "GQHOwnerAuthorizationInfoController.h"
#import "GQHDriverAuthorizationInfoController.h"
#import "GQHEnterpriseAuthorizationInfoController.h"
#import "GQHShipperAuthorizationInfoController.h"

#pragma mark CocoaPods

#pragma mark ----------Header End----------

@interface GQHAuthorizationController () <GQHAuthorizationViewDelegate> {
    
#pragma mark Data
    __block NSMutableDictionary *authorizationInfo;
#pragma mark View
    /// 自定义根视图
    GQHAuthorizationView *rootView;
}

@end


@implementation GQHAuthorizationController

#pragma mark --Lifecycle
- (void)loadView {
    DLog();
    
    rootView = [[GQHAuthorizationView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    rootView.qh_delegate = self;
    self.view = rootView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    DLog();
    
    [self fetchAuthorizationInformation];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    DLog();
    
    self.qh_titleLabel.text = @"认证信息";
    [self.qh_rightButton setHidden:YES];
    self.automaticallyAdjustsScrollViewInsets = NO;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    DLog();
    
}

#pragma mark --Delegate
- (void)qh_selectedRowWithTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath {
    
    if ([USER_GET_TYPE isEqualToString:USER_TYPE_D]) {
        
        /// 司机认证
        if (authorizationInfo) {
            
            GQHDriverAuthorizationInfoController *driverAuthorizationInfoController = [[GQHDriverAuthorizationInfoController alloc] init];
            driverAuthorizationInfoController.qh_authorizationInfo = authorizationInfo;
            [self.navigationController pushViewController:driverAuthorizationInfoController animated:YES];
        } else {
            
            [self showToastWithText:@"获取认证信息失败"];
        }
    } else if ([USER_GET_TYPE isEqualToString:USER_TYPE_O]) {
        
        /// 货主认证
        if (authorizationInfo) {
            
            GQHOwnerAuthorizationInfoController *ownerAuthorizationInfoController = [[GQHOwnerAuthorizationInfoController alloc] init];
            ownerAuthorizationInfoController.qh_authorizationInfo = authorizationInfo;
            [self.navigationController pushViewController:ownerAuthorizationInfoController animated:YES];
        } else {
            
            [self showToastWithText:@"获取认证信息失败"];
        }
    } else if ([USER_GET_TYPE isEqualToString:USER_TYPE_E]) {
        
        /// 物流企业认证
        if (authorizationInfo) {
            
            GQHEnterpriseAuthorizationInfoController *enterpriseAuthorizationInfoController = [[GQHEnterpriseAuthorizationInfoController alloc] init];
            enterpriseAuthorizationInfoController.qh_authenticatedInfo = authorizationInfo;
            [self.navigationController pushViewController:enterpriseAuthorizationInfoController animated:YES];
        } else {
            
            [self showToastWithText:@"获取认证信息失败"];
        }
    } else if ([USER_GET_TYPE isEqualToString:USER_TYPE_S]) {
        
        /// 无车承运人
        if (authorizationInfo) {
            
            GQHShipperAuthorizationInfoController *shipperAuthorizationInfoController = [[GQHShipperAuthorizationInfoController alloc] init];
            shipperAuthorizationInfoController.qh_authenticatedInfo = authorizationInfo;
            [self.navigationController pushViewController:shipperAuthorizationInfoController animated:YES];
        } else {
            
            [self showToastWithText:@"获取认证信息失败"];
        }
    } else {
        
        /// 其他
    }
}

#pragma mark --TargetMethod

#pragma mark --PrivateMethod
- (void)fetchAuthorizationInformation {
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:USER_GET_ID forKey:@"userId"];
    
    NSMutableString *URLString = [NSMutableString stringWithString:[NSString qh_APIURLString]];
    if ([USER_GET_TYPE isEqualToString:USER_TYPE_D]) {
        
        [URLString appendString:@"/authInformation/1/authDiverMsgView"];
    } else if ([USER_GET_TYPE isEqualToString:USER_TYPE_O]) {
        
        [URLString appendString:@"/authInformation/1/shipperMsgView"];
    } else if ([USER_GET_TYPE isEqualToString:USER_TYPE_E]) {
        
        [URLString appendString:@"/authInformation/1/LogisticsComMsgView"];
    } else  if ([USER_GET_TYPE isEqualToString:USER_TYPE_S]) {
        
        [URLString appendString:@"/authInformation/1/truMsgView"];
    } else {
        
        return;
    }
    
    [GQHNetHelper qh_requestGET:URLString parameters:parameters success:^(NSNumber *code, NSString *message, NSDictionary *data) {
        
        if (0 == [code intValue]) {
            
            authorizationInfo = [NSMutableDictionary dictionaryWithDictionary:data];
            
            NSMutableArray *timeArray = [NSMutableArray array];
            NSMutableArray *titleArray = [NSMutableArray array];
            
            if ([[authorizationInfo objectForKey:@"auditState"] isEqualToString:USER_STATUS_S0]) {
                
                [timeArray qh_safetySetObject:[data objectForKey:@"auditTime"] atIndex:0];
                [timeArray qh_safetySetObject:[data objectForKey:@"applyTime"] atIndex:1];
                [titleArray qh_safetySetObject:@"审核通过" atIndex:0];
                [titleArray qh_safetySetObject:@"待审核" atIndex:1];
            } else if ([[authorizationInfo objectForKey:@"auditState"] isEqualToString:USER_STATUS_S3]) {
                
                [timeArray qh_safetySetObject:[data objectForKey:@"auditTime"] atIndex:0];
                [timeArray qh_safetySetObject:[data objectForKey:@"applyTime"] atIndex:1];
                [titleArray qh_safetySetObject:@"审核未通过" atIndex:0];
                [titleArray qh_safetySetObject:@"待审核" atIndex:1];
            } else {
                
                [timeArray qh_safetySetObject:[data objectForKey:@"applyTime"] atIndex:0];
                [titleArray qh_safetySetObject:@"待审核" atIndex:0];
            }
            
            rootView.qh_timeArray = timeArray;
            rootView.qh_titleArray = titleArray;
            [rootView.qh_tableView reloadData];
        } else {
            
            [self showToastWithText:message];
        }
    } failure:^(NSError *error) {
        
        NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
        [self showToastWithText:errorString];
    }];
}

#pragma mark --Setter

#pragma mark --Getter

#pragma mark --View

@end
